<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="位置" prop="location">
          <el-input
            v-model="queryParams.location"
            placeholder="请输入位置"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="长度" prop="length">
          <el-input
            v-model="queryParams.length"
            placeholder="请输入长度"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规格" prop="specifications">
          <el-input
            v-model="queryParams.specifications"
            placeholder="请输入规格"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="购买时间">
          <el-date-picker
            v-model="daterangeBuyingTime"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="材料" prop="material">
          <el-input
            v-model="queryParams.material"
            placeholder="请输入材料"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="说明" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入说明"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="${comment}" prop="size">
          <el-input
            v-model="queryParams.size"
            placeholder="请输入${comment}"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['app:waterTunnel:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['app:waterTunnel:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['app:waterTunnel:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="waterTunnelList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
        <el-table-column label="设备名称" align="center" prop="name" />
        <!-- <el-table-column label="位置" align="center" prop="location" /> -->
        <!-- <el-table-column label="类型" align="center" prop="type" /> -->
        <el-table-column label="型号" align="center" prop="length" />
        <el-table-column label="规格" align="center" prop="specifications" />
        <el-table-column label="购买时间" align="center" prop="buyingTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.buyingTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="材料" align="center" prop="material" />
        <el-table-column label="说明" align="center" prop="code" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="大小"  align="center" prop="size" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['app:waterTunnel:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['app:waterTunnel:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改智能排水对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入设备名称" />
          </el-form-item>
          <!-- <el-form-item label="位置" prop="location">
            <el-input v-model="form.location" placeholder="请输入位置" />
          </el-form-item> -->
          <el-form-item label="选择点位">
          <el-button type="info" @click="mapPoint">选择广播坐标</el-button>
        </el-form-item>

        <el-form-item label="x坐标" prop="x">
          <el-input v-model="form.x" placeholder="请输入x坐标" />
        </el-form-item>
        <el-form-item label="y坐标" prop="y">
          <el-input v-model="form.y" placeholder="请输入坐标" />
        </el-form-item>


          <el-form-item label="型号" prop="length">
            <el-input v-model="form.length" placeholder="请输入型号" />
          </el-form-item>
          <el-form-item label="规格" prop="specifications">
            <el-input v-model="form.specifications" placeholder="请输入规格" />
          </el-form-item>
          <el-form-item label="购买时间" prop="buyingTime">
            <el-date-picker clearable
              v-model="form.buyingTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择购买时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="材料" prop="material">
            <el-input v-model="form.material" placeholder="请输入材料" />
          </el-form-item>
          <el-form-item label="说明" prop="code">
            <el-input v-model="form.code" placeholder="请输入说明" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
          <el-form-item label="大小" prop="size">
            <el-input v-model="form.size" placeholder="请输入大小" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog :visible.sync="openMap" append-to-body>
      <div style="font-size: 20px">
        <span style="margin-right: 20px">X坐标:{{ form.x }}</span
        ><span>Y坐标:{{ form.y }}</span>
      </div>
      <div id="openMap" style="height:70vh"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openMap = false">确 定</el-button>
        <el-button @click="openMap = false">取 消</el-button>
      </div>
    </el-dialog>
    </div>
  </template>
  
  <script>
  import { listWaterTunnel, getWaterTunnel, delWaterTunnel, addWaterTunnel, updateWaterTunnel } from "@/api/app/waterTunnel";
import { bd09towgs84,wgs84tobd09 } from "@/utils/bub";
  
  export default {
    name: "WaterTunnel",
    data() {
      return {
        // 遮罩层
        loading: true,
        openMap:false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 智能排水表格数据
        waterTunnelList: [],
        imgBalance: require("@/assets/s/position.png"),
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // $comment时间范围
        daterangeBuyingTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          location: null,
          type: 2,
          length: null,
          specifications: null,
          buyingTime: null,
          material: null,
          code: null,
          size: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
        mapPoint() {
      this.openMap = true;
      this.$nextTick(() => {
        this.Map = new BMap.Map("openMap", { mapType: BMAP_SATELLITE_MAP }); // 创建Map实例

        console.log(this.Map);
        this.Map.centerAndZoom(new BMap.Point(114.608347, 24.120331), 16); // 初始化地图,设置中心点坐标和地图级别
        this.Map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

        this.Map.addEventListener("click", (e) => {
          // console.log(e.point.lng + "," + e.point.lat);
          this.Map.clearOverlays();
          this.form.location = bd09towgs84(e.point.lng,e.point.lat)[0] +","+ bd09towgs84(e.point.lng,e.point.lat)[1];
          this.form.x = bd09towgs84(e.point.lng,e.point.lat)[0];
          this.form.y = bd09towgs84(e.point.lng,e.point.lat)[1];
          console.log(this.form.location);
          let pitA = new BMap.Point(e.point.lng, e.point.lat);
          var myIcon = new BMap.Icon(
            this.imgBalance,
            new BMap.Size(30, 30)
          );
          myIcon.setImageSize(new BMap.Size(30, 30));
          var marker = new BMap.Marker(pitA, {
            icon: myIcon,
          });
          this.Map.addOverlay(marker);
        });
        
      });
    },
      /** 查询智能排水列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeBuyingTime && '' != this.daterangeBuyingTime) {
          this.queryParams.params["beginBuyingTime"] = this.daterangeBuyingTime[0];
          this.queryParams.params["endBuyingTime"] = this.daterangeBuyingTime[1];
        }
        listWaterTunnel(this.queryParams).then(response => {
          this.waterTunnelList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          location: null,
          type: 2,
          length: null,
          specifications: null,
          buyingTime: null,
          material: null,
          code: null,
          remark: null,
          size: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeBuyingTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加智能排水";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getWaterTunnel(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改智能排水";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWaterTunnel(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWaterTunnel(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除智能排水编号为"' + ids + '"的数据项？').then(function() {
          return delWaterTunnel(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('app/waterTunnel/export', {
          ...this.queryParams
        }, `waterTunnel_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  
